from elasticsearch import Elasticsearch
import os
import pathlib
import sys
project_path = pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent
sys.path.append(str(project_path))
from etc.question_answer_config import attr_index_name,value_index_name

es = Elasticsearch()

#########
# attr_index_name
#########

# 若更改了同义词字典，则更新search_analyzer
es.indices.reload_search_analyzers(attr_index_name)

# 测试分词结果
question = '的房屋楼盘类别'
analyze = es.indices.analyze(index=attr_index_name,
                   body={
                       'text': question,
                       'analyzer': 'ik_max_syn'
                   }
                   )
print(f'==============="{question}"分词===============')
[print(m['token']) for m in analyze['tokens']]
print(f'==============================')

# 测试检索结果
search_object = {'query': {'match': {'value': question}},'size':5}
#search_object = {'query': {'multi_match': {'query': question, 'fields': ['value', 'table']}},'size':5}
search = es.search(index=attr_index_name, body=search_object)
print(f'==============="{question}"查询===============')
[print(m['_score'], m['_source']) for m in search['hits']['hits']]
print(f'==============================')

#########
# value_index_name
#########
